package com.scau.controller;

import com.scau.dto.CBECReturn;
import com.scau.exception.FileUtilException;
import com.scau.exception.GoodsStockException;
import com.scau.exception.OrderItemException;
import com.scau.pojo.Brand;
import com.scau.pojo.CompanyInfo;
import com.scau.pojo.Goods;
import com.scau.pojo.OrderItem;
import com.scau.dto.OrderInfo;
import com.scau.service.IBrandControllerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 *  BrandController
 *
 * Siaoja
 */

@Controller
@CrossOrigin
@RequestMapping("")
public class BrandController {

    @Autowired
    private IBrandControllerService brandControllerService;

//    主页请求
    //品牌商-公司信息  --显示公司信息
    @RequestMapping(value = "/brand/company/{username}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn showCompanyInformation(@PathVariable String username) {

        CompanyInfo companyInfo = brandControllerService.getCompanyInfo(username);

        return CBECReturn.ok(companyInfo);
    }

    //品牌商-品牌管理      --获得所有品牌
    @RequestMapping(value = "/brand/brand/username/{username}", method = RequestMethod.GET,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn showBrands(@PathVariable("username") String username) {
        List<Brand> brandList = brandControllerService.getBrands(username);

        return CBECReturn.ok(brandList);
    }

    //品牌商-商品录入,品牌商-商品主图     --显示所有商品列表
    @RequestMapping(value = "/brand/goods/{username}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn showGoods(@PathVariable String username) {
        List<Goods> goodsList = brandControllerService.getGoods(username);

        return CBECReturn.ok(goodsList);
    }

    //品牌商-订单管理      --返回所有订单信息
    @RequestMapping(value = "/brand/order/{username}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn ordersManagement(@PathVariable String username){
        List<OrderInfo> orderInfoList = brandControllerService.ordersManagement(username);

        return CBECReturn.ok(orderInfoList);
    }

    //品牌商-我的信息
    //保存新增的公司信息
    @RequestMapping(value = "/brand/company",method = RequestMethod.POST,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn saveCompanyInfo(CompanyInfo companyInfo){
        //保存公司信息
        brandControllerService.saveCompanyInfo(companyInfo);

        return CBECReturn.ok();
    }
    //保存修改的公司信息
    @RequestMapping(value = "/brand/company/{username}",method = RequestMethod.POST,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn updateCompanyInfo(@PathVariable("username")String username, CompanyInfo companyInfo){
        companyInfo.setUsername(username);
        //保存公司信息
        brandControllerService.saveCompanyInfo(companyInfo);

        return CBECReturn.ok();
    }

    //品牌商-品牌管理
    //  --增加品牌
    @RequestMapping(value = "/brand/brand",method = RequestMethod.POST)
    @ResponseBody
    public CBECReturn addBrand(Brand brand, @RequestParam("logo") MultipartFile logo) throws IOException {
        brandControllerService.addBrand(brand,logo);
        return CBECReturn.ok();
    }
    //  --获得修改品牌信息
    @RequestMapping(value = "/brand/brand/id/{id}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn getUpdateBrand(@PathVariable("id") String id){
        Brand brand = brandControllerService.getUpdateBrand(id);
        return CBECReturn.ok(brand);
    }
    //  --修改品牌信息
    @RequestMapping(value = "/brand/brand/{id}",method = RequestMethod.POST,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn getUpdateBrand(@PathVariable("id")String id, Brand brand,@RequestParam(value = "logo",required = false)MultipartFile logo) throws IOException {
        brand.setId(id);
        System.out.println("Brand is :" + brand);
        brandControllerService.updateBrand(brand,logo);
        return CBECReturn.ok(brand);
    }
    //  --删除品牌信息
    @RequestMapping(value = "/brand/brand/{id}",method = RequestMethod.DELETE,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn deleteBrand(@PathVariable("id") String id) throws FileUtilException {
        brandControllerService.deleteBrand(id);
        return CBECReturn.ok();
    }

    //品牌商-商品录入
    /*//新增按钮，
    @RequestMapping(value = "/brand/goods/username/{username}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn toAddGoods(@PathVariable("username")String username){
        //查询要被更新商品原来的值，返回
        List<Brand> brandList = brandControllerService.getBrands(username);
        return CBECReturn.ok(brandList);
    }*/
    //修改按钮，跳转至修改页面
    @RequestMapping(value = "/brand/goods/id/{id}",method = RequestMethod.PUT,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn toUpdateGoods(@PathVariable("id")String id){
		//查询要被更新商品原来的值，返回
        Goods goods = brandControllerService.getUpdateGoods(id);

        return CBECReturn.ok(goods);
    }
    //删除按钮,删除
    @RequestMapping(value = "/brand/goods/id/{id}",method = RequestMethod.DELETE,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn DeleteGoods(@PathVariable("id")String id) throws FileUtilException {
        Goods goods = brandControllerService.deleteGoods(id);

        return CBECReturn.ok(goods);
    }
    //修改商品状态
    @RequestMapping(value = "/brand/goods/updateStatus/id/{id}",method = RequestMethod.PUT,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn updateGoodsStatus(@PathVariable("id")String id,Goods goods){
        goods.setId(id);
        brandControllerService.updateBrandGoodsStatus(goods);
        return CBECReturn.ok();
    }

    //品牌商-订单管理
    //查询某个订单
    @RequestMapping(value = "/brand/oreder/id/{id}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn getBrandOrder(@PathVariable("id") String id) {
        OrderInfo orderInfo = brandControllerService.getBrandOrder(id);

        return CBECReturn.ok(orderInfo);
    }
    //更新订单状态
    @RequestMapping(value = "/brand/order/id/{id}",method = RequestMethod.PUT,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn updateOrderStatus(@PathVariable("id") String id,@RequestBody OrderItem orderItem) throws GoodsStockException, OrderItemException {

//        System.out.println("id:" + id + "status:" + status);
        brandControllerService.updateBrandOrderStatus(id, orderItem.getStatus());
        return CBECReturn.ok();
    }
}
